package com.mialab.servlet_demo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/FormServlet")
public class FormServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public FormServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置响应到客户端MIME类型和字符编码方式
		response.setContentType("text/html;charset=UTF-8");

		// 设置request对象的解码方式
		request.setCharacterEncoding("utf-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String sex = request.getParameter("sex");
		String home = request.getParameter("home");
		String info = request.getParameter("info");

		// 获取输出流
		PrintWriter out = response.getWriter();
		out.println("<p>用户名：" + username + "</p>");
		out.println("<p>密码：" + password + "</p>");
		out.println("<p>性别：" + sex + "</p>");
		out.println("<p>家乡：" + home + "</p>");
		out.println("<p>爱好：");

		// 获取参数名为“hobby”的值
		String[] hobbys = request.getParameterValues("hobby");
		for (int i = 0; i < hobbys.length; i++) {
			if (i < hobbys.length - 1)
				out.println(hobbys[i] + "，");
			else
				out.println(hobbys[i] + "");
		}
		out.println("</p>");
		out.println("<p>自我介绍：" + info + "</p>");
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
